package org.liveboardgames.agricola.control.delegates.action.prerequisite
{
	import org.liveboardgames.agricola.control.delegates.helper.GameHelper;
	import org.liveboardgames.agricola.model.AgricolaModelLocator;
	import org.liveboardgames.agricola.vo.card.ImprovementCard;
	import org.liveboardgames.agricola.vo.player.Player;
	import org.liveboardgames.common.dictionary.collections.IMap;
	
	import mx.collections.ListCollectionView;
	
	public class BuildMajorImprovementPrerequisiteChecker extends PrerequisiteChecker
	{				
		public function BuildMajorImprovementPrerequisiteChecker(player:Player)
		{
			super(player);
		}
		
		override public function check():Boolean
		{
			var result:Boolean = false;
			
			// Check that there is at least one major improvement to build
			var majorImprovements:ListCollectionView = AgricolaModelLocator.getInstance().game.majorImprovements;
			if (majorImprovements != null && majorImprovements.length != 0)
			{
				// Check that the player can afford at least one of them
				for each (var card:ImprovementCard in majorImprovements)
				{
					var cost:IMap = card.cost;
					var alternativeCosts:ListCollectionView = card.alternativeCosts;
					if (GameHelper.canPay(player, cost, alternativeCosts))
					{
						result = true;
						break;
					}
				}
			}
			
			return result;
		}
		
	}
}